<?php

/**
 * @author Kevin
 * @version 1.0
 * @created 12-d�c.-2011 19:51:41
 */
class User
{

	/**
	 * Déclarations des données
	 */
	var $id = NULL;
	var $mail = NULL;
	var $nickname = NULL;
	var $options = Array();
	var $password = NULL;
	var $subscribeDate = NULL;


	/**
	 * Constructeur
	 * 
	 * @param pValue
	 * @param pMethod
	 */
	function User($pValue = "", $pMethod = "id")
	{
		switch ($pMethod){
			case "id" :
				$this->LoadFromID($pValue);
				break;
			case "mail" :
				$this->LoadFromMail($pValue);
				break;
		}
	}
	
	function LoadFromID($pId){
		global $wpdb;
	
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM users WHERE id='%d' ", $pId));
		$this->extractRowInformation($_row);
	}
	
	function LoadFromMail($pMail){
		global $wpdb;
	
		$_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM users WHERE mail='%s' ", $pMail));
		$this->extractRowInformation($_row);
	}
	
	function extractRowInformation($pRow){
		global $wpdb;
	
		$_row = $pRow;
		$this->id = $_row->id;
		$this->nickname = $_row->nickname;
		$this->mail = $_row->mail;
		$this->password = $_row->password;
		$this->subscribeDate = $_row->subscribedate;
	
		// recuperation des options
		$_options = $wpdb->get_results($wpdb->prepare("SELECT * from useroptions WHERE fk_user = %d", $this->id));
		foreach($_options as $_option){
			$this->options[$_option->name] = $_option->value;
		}
	}

	/**
	 * ====================== Création ======================
	 * 
	 * @param pNickname
	 * @param pMail
	 * @param pPassword
	 */
	function create($pNickname, $pMail, $pPassword)
	{
		global $wpdb;
		
		// creation de l'utilisateur
		$queryTable = 'users';
		$queryData = array(
					'nickname' => $pNickname, 
					'mail' => $pMail,
					'password' => md5($pPassword),
					'subscribedate' => date("Y-m-d"),
					'activated' => 0
		);
		$queryDataFormat = array('%s','%s','%s','%s');
		$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
		
		$this->LoadFromID($wpdb->insert_id);
		
		// creation des options utilisateur de base
		$this->setOption("displayTicketInFeed", "enable");
		$this->setOption("displayStatusInFeed", "enable");
		$this->setOption("displayRelationshipInFeed", "enable");
		$this->setOption("displayNewsInFeed", "enable");
		
		// creation du code d'activation du compte
		$queryTable = 'useractivations';
		$queryData = array(
					'fk_user' => $this->id, 
					'code' => md5($pNickname.$pMail.$pPassword),
		);
		$queryDataFormat = array('%d','%s');
		$wpdb->insert( $queryTable, $queryData, $queryDataFormat);
			
		return $this->id;
	}

	/**
	 * 
	 * @param name
	 */
	function delOption($name)
	{
	}


	/**
	 * ====================== Get/Set ======================
	 */
	public function setId($id)
	{
	    $this->id = $id;
	}
	
	public function getId()
	{
	    return $this->id;
	}

	public function setMail($mail)
	{
	    $this->mail = $mail;
	}
	
	public function getMail()
	{
	    return $this->mail;
	}
	

	function getMostCommonVisitors()
	{
	}

	function getMostVisitedProfiles()
	{
	}

	public function setNickname($nickname)
	{
	    $this->nickname = $nickname;
	}
	
	public function getNickname()
	{
	    return $this->nickname;
	}

	public function setPassword($password)
	{
	    $this->password = $password;
	}
	
	public function getPassword()
	{
	    return $this->password;
	}

	function getRelationship()
	{
	}

	function getRelationshipDemands()
	{
	}

	public function setOption($optionName, $optionValue)
	{
		// utilisé la classe option
	}
	
	public function getOption($optionName)
	{
	    return $this->optionName;
	}

}
?>